package com.turing.manage.grade;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GradeServlet extends HttpServlet {
	
	GradeService gradeService = new GradeService();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			String method = request.getParameter("method");
			
			if ("query".equals(method))
			{
				this.query(request,response);
			}
			
		} catch (ClassNotFoundException | SQLException e) {
			System.out.println("1.发送的异常信息为:"+e.getMessage());
			System.out.println("2.发送异常的内存地址为:"+e.getStackTrace());
			System.out.println("3.转向到错误处理页面");
			request.setAttribute("errorMsg",e.getMessage());
			request.getRequestDispatcher("/manage/error.jsp").forward(request, response);
		}
	}

		
	
/**
 *  desc 查询全部
 * @param request
 * @param response
 * @throws ClassNotFoundException
 * @throws SQLException
 * @throws ServletException
 * @throws IOException
 */
	private void query(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
		
	     String grade_id = request.getParameter("grade_id");
	        System.out.println(grade_id);
	        String key = request.getParameter("key");


	        List<Map<String, Object>> list_grade = gradeService.queryByAll(grade_id, key);


	        request.setAttribute("list_grade", list_grade);
	        request.setAttribute("grade_id", grade_id);
	        request.setAttribute("key", key);
		
		request.getRequestDispatcher("/manage/grade/list.jsp").forward(request, response);
		
	}
	
	
}
